<template>
  <div>
    <el-table :data="shopTypes"  style="width: 50%">
      <el-table-column prop="name" label="分类名" ></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="shopType.name"
            size="mini"
            @blur="search"
            placeholder="输入分类名搜索"/>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="openEditWindow(scope.row.id)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="shopTypeDel(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageno"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <div>
      <el-dialog
        title="编辑"
        :visible.sync="dialogVisible"
        width="40%"
        :before-close="handleClose"
      >
        <component ref="editShopType" is="EditShopType"></component>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import EditShopType from "./EditShopType";
  export default {
    name: "ShopTypeView",
    data() {
      return {
        shopTypes: [],
        shopType: {name: ""},
        pageno: 1,
        pagesize: 5,
        total: 1,
        dialogVisible:false
      }
    },
    methods: {
      getTypes() {
        var params = {}
        params["pageno"] = this.pageno;
        params["pagesize"] = this.pagesize;
        Object.keys(this.shopType).forEach(item => {
          params[item] = this.shopType[item];
        });
        this.$axios.get("shopType/allShopType.action", {params: params})
          .then(res => {
            this.shopTypes = res.data.records;
            this.total = res.data.total;
          })
          .catch(err => {
            this.$message.error(err)
          })
      },
      search() {
        this.getTypes();
      },
      openEditWindow(id) {
        this.dialogVisible = true;
        this.$nextTick(item=>{
          this.$refs.editShopType.getdata(id);
        })

      },
      handleClose(done) {
        //模态框的关闭
        this.$confirm("确认关闭？")
          .then((_) => {
            done();
          })
          .catch((_) => {
          });
      },
      shopTypeDel(id) {
        //删除
        this.$confirm('确认删除该分类吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         this.$axios
          .delete("shopType/delShopType.action/"+id)
          .then(res=>{
            if (res.data.code>0){
              this.$message.success(res.data.msg)
              this.getTypes();
            }else{
              this.$message.error(res.data.msg)
            }
          })
          .catch(err=>{this.$message.error(err)})
        }).catch();
      },
      handleSizeChange(val) {
        //每页范围
        this.pagesize = val;
        this.getTypes();
      },
      handleCurrentChange(val) {
        //当前页
        this.pageno = val;
        this.getTypes();
      },
    },
    components: {
      EditShopType
    },
    created() {
      this.getTypes();
    }
  }
</script>

<style scoped>

</style>
